package com.yj.testbook_yj.controller;

import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.yj.testbook_yj.domain.TextbookInfo;
import com.yj.testbook_yj.service.TextbookService;
import com.yj.testbook_yj.utils.UUIDUtil;
import com.yj.testbook_yj.vo.TextbookInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/try")
@EnableAutoConfiguration
public class TextbookController {
    @Autowired
    private TextbookService textbookService;


    @GetMapping("/findAll")
    public List<TextbookInfo> findAll(){
        return textbookService.findAll();
    }

    @RequestMapping("/addTextbookInfo")
    public String addTextbookInfo(){

    // 传入网卡信息，基于时间制作出生成器
        TimeBasedGenerator generator = Generators.timeBasedGenerator(EthernetAddress.fromInterface());
        UUID uuid = generator.generate();
        // 通过计算当前时间戳、随机数和机器MAC地址得到，由于使用了MAC地址，因此这个版本的随机数能够保证全球唯一
        System.out.println(uuid.version());
        System.out.println("");
        TextbookInfo textbookInfo = new TextbookInfo();
        textbookInfo.setUuid(uuid.toString().replaceAll("-", ""));
        textbookInfo.setTextbookName("自学语文");
        textbookInfo.setTextbookGrade("初中");
        textbookInfo.setTextbookSubject("语文");
        textbookInfo.setTextbookVersion("3.45");
        textbookInfo.setGradeCode("cz");
        textbookInfo.setSubjectCode("yw");
        textbookInfo.setVersionCode("sansiwu");
        return textbookService.addTextbookInfo(textbookInfo);
    }

    @PutMapping("/updateTextbookInfo")
    public String updateTextbookInfo(){
        TextbookInfo textbookInfo = new TextbookInfo();
        textbookInfo.setTextbookId(4);
        textbookInfo.setUuid(UUIDUtil.getUuid());
        textbookInfo.setTextbookName("自学数学");
        textbookInfo.setTextbookGrade("大学");
        textbookInfo.setTextbookSubject("数学");
        textbookInfo.setTextbookVersion("3.47");
        textbookInfo.setGradeCode("dx");
        textbookInfo.setSubjectCode("sx");
        textbookInfo.setVersionCode("sansiqi");
        return textbookService.updateTextbookInfo(textbookInfo);
    }

    @DeleteMapping("/delTextbookInfo")
    public String delTextbookInfo(){
        return textbookService.delTextbookInfo("自学数学");
    }

    @RequestMapping("/getTextbookInfoVO")
    public List<TextbookInfoVO> getTextbookInfoVO(){
        List<TextbookInfoVO> textbookInfoVO = textbookService.getTextbookInfoVO();
        return textbookInfoVO;
    }
}
